﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HTTT.Bussiness;
using HTTT.DataAccess;
using HTTT.AD;
using System.Configuration;

namespace HTTT.admin.controls
{
    public partial class ucEditUserRequestDetail : System.Web.UI.UserControl
    {
        private Guid _requestID;
        public Guid RequestID
        {
            get { return _requestID; }
            set { _requestID = value; /*LoadData();*/ }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            LoadRequestDetail();
            string ouPathNewUser = LoadNewEditUserDetail();
            LoadOU(ddlOU, ouPathNewUser);
            string ouPathOldUser = LoadOldEditUserDetail();
            LoadOU(ddlOUOld, ouPathOldUser);
            SetReadOnly(true);
        }

        private void LoadRequestDetail()
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(RequestID);
            ltlActionName.Text = MyRequest.ActionName;
            txtFromRequest.Text = MyRequest.Note;
            txtRequestedBy.Text = MyRequest.Requestedby;
            txtRequestedDate.Text = MyRequest.RequestDate.ToString();
        }

        private void SetReadOnly(bool IsReadOnly)
        {
            // Get all TextBoxes and set the value of the ReadOnly property.
            foreach (var tb in this.Controls.OfType<TextBox>())
                tb.ReadOnly = IsReadOnly;
            ddlOU.Enabled = !IsReadOnly;
            ddlOUOld.Enabled = !IsReadOnly;
            chklAccountOptions.Enabled = !IsReadOnly;
            chklAccountOptionsOld.Enabled = !IsReadOnly;
        }

        private string LoadOldEditUserDetail()
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(RequestID);
            
            HT_UserBO UserBO = new HT_UserBO();
            HT_User UserInfo = UserBO.GetOldHTUserFromEditUserData(MyRequest.ActionDescription);
            txtgivenNameOld.Text = UserInfo.givenName;
            txtmidleNameOld.Text = UserInfo.initials;
            txtsurNameOld.Text = UserInfo.sn;
            txtDisplayNameOld.Text = UserInfo.displayName;
            txtDescriptionOld.Text = UserInfo.description;
            txtMailOld.Text = UserInfo.mail;
            txtHomePageOld.Text = UserInfo.wWWHomePage;
            txtUserNameOld.Text = UserInfo.sAMAccountName;
            txtJobOld.Text = UserInfo.title;
            txtPhysicalDeliveryOfficeNameOld.Text = UserInfo.physicalDeliveryOfficeName;
            txtCompanyOld.Text = UserInfo.company;
            txtDepartmentOld.Text = UserInfo.department;
            txtAddress1Old.Text = UserInfo.streetAddress;
            txtAddress2Old.Text = UserInfo.postOfficeBox;
            txtPostalcodeOld.Text = UserInfo.postalCode;
            txtCityOld.Text = UserInfo.l;
            txtProvinceOld.Text = UserInfo.st;
            txtCountryOld.Text = UserInfo.c;
            txtHomephoneOld.Text = UserInfo.homephone;
            txtMobileOld.Text = UserInfo.mobile;
            txtOfficephoneOld.Text = UserInfo.voiceTelephoneNumber;
            txtFaxOld.Text = UserInfo.facsimileTelephoneNumber;
            txtIpphoneOld.Text = UserInfo.ipPhone;
            //txtNote.Text = UserInfo.info;
            long AccountOptions = (long)UserInfo.userAccountControl;
            foreach (ListItem i in chklAccountOptionsOld.Items)
            {
                if ((AccountOptions & Convert.ToInt64(i.Value)) > 0)
                {
                    i.Selected = true;
                }
            }
            string ouPath = UserInfo.distinguishedName;
            ouPath = ouPath.Substring(ouPath.IndexOf(',') + 1);
            return ouPath;
        }


        private void LoadOU(DropDownList ddl, string selectedValue)
        {
            //Get OU list
            HT_OUBO Bo = new HT_OUBO();
            List<HT_OU> ListBO = Bo.GetAllHT_OU().ToList();
            ddl.DataSource = ListBO;
            ddl.DataValueField = ADOUProperties.DISTINGUISHEDNAME;
            ddl.DataTextField = ADOUProperties.NAME;
            ddl.DataBind();
            ddl.Items.Insert(0, new ListItem("▼ " + ConfigurationManager.AppSettings["LDAPDomain"], ConfigurationManager.AppSettings["DomainPath"]));
            ddl.SelectedValue = selectedValue;
        }
        private string LoadNewEditUserDetail()
        {
            HT_RequestBO BO = new HT_RequestBO();
            HT_Request MyRequest = BO.GetHT_RequestById(RequestID);
            HT_UserBO UserBO = new HT_UserBO();
            HT_User UserInfo = UserBO.GetNewHTUserFromEditUserData(MyRequest.ActionDescription);
            txtgivenName.Text = UserInfo.givenName;
            txtmidleName.Text = UserInfo.initials;
            txtsurName.Text = UserInfo.sn;
            txtDisplayName.Text = UserInfo.displayName;
            txtDescription.Text = UserInfo.description;
            txtMail.Text = UserInfo.mail;
            txtHomePage.Text = UserInfo.wWWHomePage;
            txtUserName.Text = UserInfo.sAMAccountName;
            txtJob.Text = UserInfo.title;
            txtPhysicalDeliveryOfficeName.Text = UserInfo.physicalDeliveryOfficeName;
            txtCompany.Text = UserInfo.company;
            txtDepartment.Text = UserInfo.department;
            txtAddress1.Text = UserInfo.streetAddress;
            txtAddress2.Text = UserInfo.postOfficeBox;
            txtPostalcode.Text = UserInfo.postalCode;
            txtCity.Text = UserInfo.l;
            txtProvince.Text = UserInfo.st;
            txtCountry.Text = UserInfo.c;
            txtHomephone.Text = UserInfo.homephone;
            txtMobile.Text = UserInfo.mobile;
            txtOfficephone.Text = UserInfo.voiceTelephoneNumber;
            txtFax.Text = UserInfo.facsimileTelephoneNumber;
            txtIpphone.Text = UserInfo.ipPhone;
            //txtNote.Text = UserInfo.info;
            long AccountOptions = (long)UserInfo.userAccountControl;
            foreach (ListItem i in chklAccountOptions.Items)
            {
                if ((AccountOptions & Convert.ToInt64(i.Value)) > 0)
                {
                    i.Selected = true;
                }
            }
            string ouPath = UserInfo.distinguishedName;
            ouPath = ouPath.Substring(ouPath.IndexOf(',') + 1);
            return ouPath;
        }

    }
}